/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Lionel Prado
 */
@Entity
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CompteBancaire.findAll", query = "SELECT cb FROM CompteBancaire cb"),
    @NamedQuery(name = "CompteBancaire.findAllDifferentCbCourant", query = "SELECT cb FROM CompteBancaire cb WHERE cb.proprietaireCompte != :user"),
    @NamedQuery(name = "CompteBancaireEpargne.findAll", query = "SELECT cbe FROM CompteBancaireEpargne cbe"),
    @NamedQuery(name = "CompteBancaire.findCompteByUser", query = "SELECT cb FROM CompteBancaire cb, Utilisateur user WHERE cb.proprietaireCompte = :user"),
    @NamedQuery(name = "CompteBancaire.findCompteByUserDiffCompte", query = "SELECT cb FROM CompteBancaire cb, Utilisateur user WHERE cb.proprietaireCompte = :user AND cb.id != :idPass"),
    @NamedQuery(name = "CompteBancaire.findCompteJointByUser", query = "SELECT cbj FROM CompteBancaireJoint cbj, Utilisateur user WHERE cbj.proprietaireCompte2 = :user")
})

public class CompteBancaire implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
//    private String nomProprietaire;
    private float soldeCompte;
    private String description = "Compte courant";
    
    
    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER) 
    private Collection<OperationBancaire> listeOperationBancaire = new <OperationBancaire>ArrayList();
    @ManyToOne
    private Utilisateur proprietaireCompte;
    
    public CompteBancaire() {
    }
    
    public CompteBancaire(float solde) {
        this.soldeCompte = solde;
    }
    
     public CompteBancaire(Utilisateur proprietaireCompte, float soldeCompte) {
        this.proprietaireCompte = proprietaireCompte;
        this.soldeCompte = soldeCompte;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public Utilisateur getProprietaireCompte() {
        return proprietaireCompte;
    }

    public void setProprietaireCompte(Utilisateur proprietaireCompte) {
        this.proprietaireCompte = proprietaireCompte;
    }

    public Collection<OperationBancaire> getOperations() {  
        return listeOperationBancaire;  
    }  
  
    public void setOperations(Collection<OperationBancaire> listeOperationBancaire) {  
        this.listeOperationBancaire = listeOperationBancaire;  
    }  
    
    public void addOperation(OperationBancaire op){
        this.listeOperationBancaire.add(op);
    }
    
    public float getSoldeCompte() {
        return soldeCompte;
    }

    public void setSoldeCompte(float soldeCompte) {
        this.soldeCompte = soldeCompte;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    //credit d'un compte    
    public void ajouterSolde(float soldeTransfert) {
        System.out.println("ajouter solde !!!" + soldeTransfert);
        System.out.println("AVANT solde !!!" + this.soldeCompte);
        this.soldeCompte = this.soldeCompte + soldeTransfert;
        System.out.println("APRES solde !!!" + this.soldeCompte);
    }
    
    //debit d'un compte
    public void soustraireSolde(float soldeTransfert) {
        System.out.println("soustraire solde !!!");
        System.out.println("AVANT solde !!!" + this.soldeCompte);
        this.soldeCompte = this.soldeCompte - soldeTransfert;
        System.out.println("APRES solde !!!" + this.soldeCompte);
    }
    
    
    public void addUser(Utilisateur u){
        this.proprietaireCompte = u;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CompteBancaire)) {
            return false;
        }
        CompteBancaire other = (CompteBancaire) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.CompteBancaire[ id=" + id + " ]";
    }
    
}
